package com.xiaochong.tir.api.filter;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xc.logclient.filter.InternalApiTraceFilter;

import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.api.service.BtUserService;
import com.xiaochong.tir.api.userclient.UserServiceClient;
import com.xiaochong.tir.common.data.mapper.BtUserMapper;
import com.xiaochong.tir.common.data.vo.BtUser;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author cx
 * @date 2018-02-09 15:23
 * Created by cx on 2018/2/9 0009.
 */
@WebFilter(filterName="apitrace",urlPatterns="/*", initParams={@WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico")})
public class ApiTraceFilter extends InternalApiTraceFilter {

    private final static Logger logger = LoggerFactory.getLogger(ApiTraceFilter.class);

    @Autowired
    BtUserService btUserService;

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        logger.info("request>>>>>"+req.getRemoteAddr()+"||"+req.getRemoteHost()+"||"+req.getRemotePort()+"||"+request.getRequestURI());
        String userToken = request.getParameter( "userToken" );
        if(StringUtils.isNotBlank( userToken )){
            btUserService.setUserCache( userToken );
        }
        super.doFilter(req ,resp ,chain);

    }
}
